package com.mind.customer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mind.cache.lock.annotation.DMLock;
import com.mind.common.utils.DateUtils;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjCallApiDetailMapper;
import com.mind.customer.mapper.AjCallApiMapper;
import com.mind.system.mapper.AttachResultMapper;
import com.mind.customer.utils.CountUtils;
import com.mind.system.domain.AjAttachResult;
import com.mind.system.domain.AjCallApiDetail;
import com.mind.system.domain.AjTicketTask;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Log4j2
public class SyncCallApiService {

    @Resource
    private AjCallApiDetailMapper callApiDetailMapper;

    @Resource
    private AjCallApiMapper callApiMapper;

    @Resource
    private CountUtils countUtils;

    @Resource
    private AttachResultMapper attachResultMapper;

    private Map<String, String> urlMapping = new HashMap<String, String>() {{
        put("call_ocr", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_ppocr");
        put("call_model", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_check_PI");
        put("call_stamp", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_check_stamp");
    }};

    @DMLock(key = "callApiByAttach")
    public void callApiByAttach() {
        List<String[]> monthList = new ArrayList<String[]>(){{
            add(new String[]{"2024-09-01 00:00:00", "2024-09-31 23:59:59"});
            add(new String[]{"2024-10-01 00:00:00", "2024-10-31 23:59:59"});
            add(new String[]{"2024-11-01 00:00:00", "2024-11-15 23:59:59"});
        }};
        for (String[] month : monthList) {
            log.info("开始同步{}, {}的附件", month[0], month[1]);
            syncCallApiByAttachDate(month[0], month[1]);
        }
    }

    private void syncCallApiByAttachDate(String startDate, String endDate) {
        QueryWrapper<AjAttachResult> wrapper = new QueryWrapper<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        wrapper.ne("status", "发票");
        wrapper.ge("create_time", LocalDateTime.parse(startDate, formatter));
        wrapper.le("create_time", LocalDateTime.parse(endDate, formatter));
        wrapper.eq("`trigger`", 0);
        List<AjAttachResult> ajAttachResults = attachResultMapper.selectList(wrapper);
        String month = DateUtils.parseDateToStr("yyyy-MM",DateUtils.parseDate(startDate));
        for(AjAttachResult attachResult : ajAttachResults){
            syncCallApiByAttach(attachResult, month);
        }

    }

    private void syncCallApiByAttach(AjAttachResult attachResult, String month) {
        if(StringUtils.isNotEmpty(attachResult.getStatus())) {
            if(!attachResult.getStatus().equals("没有检测到附件分类")) {
                countUtils.countApi("call_model", attachResult.getResult(), 1, attachResult.getFileUrl(), new AjTicketTask() {{
                    setTicketId(attachResult.getTicketId());
                    setVersion(attachResult.getVersion());
                }}, month);
            }
            if(attachResult.getStatus().contains("合同")){
                countUtils.countApi("call_stamp", "", 1, attachResult.getFileUrl(), new AjTicketTask() {{
                    setTicketId(attachResult.getTicketId());
                    setVersion(attachResult.getVersion());
                }}, month);
            }
        }
        attachResult.setTrigger(1);
        attachResultMapper.updateById(attachResult);
    }
    @DMLock(key = "callApiByDetail")
    public void callApiByDetail() {
        List<String[]> monthList = new ArrayList<String[]>(){{
            add(new String[]{"2024-11-15 00:00:00", "2024-11-30 23:59:59"});
            add(new String[]{"2024-12-01 00:00:00", "2024-12-30 23:59:59"});
        }};
        for (String[] month : monthList) {
            log.info("开始同步{}, {}的detail", month[0], month[1]);
            syncCallApiByDetailDate(month[0], month[1]);
        }
    }

    public void syncCallApiByDetailDate(String startDate, String endDate){
        List<AjCallApiDetail> callOcrDetails = callApiDetailMapper.selectCallApiDetailList(new AjCallApiDetail() {{
            setCallApiId(5);
            setStartCreateTime(startDate);
            setEndCreateTime(endDate);
            setTrigger(0);
        }});
        List<AjCallApiDetail> callModelDetails = callApiDetailMapper.selectCallApiDetailList(new AjCallApiDetail() {{
            setCallApiId(4);
            setStartCreateTime(startDate);
            setEndCreateTime(endDate);
            setTrigger(0);
        }});
        List<AjCallApiDetail> callStampDetails = callApiDetailMapper.selectCallApiDetailList(new AjCallApiDetail() {{
            setCallApiId(6);
            setStartCreateTime(startDate);
            setEndCreateTime(endDate);
            setTrigger(0);
        }});
        String month = DateUtils.parseDateToStr("yyyy-MM",DateUtils.parseDate(startDate));
        callApiByDetail(callOcrDetails, month, "call_ocr");
        callApiByDetail(callModelDetails, month, "call_model");
        callApiByDetail(callStampDetails, month, "call_stamp");
    }

    private void callApiByDetail(List<AjCallApiDetail> callOcrDetails, String month, String type) {
        for(AjCallApiDetail callOcrDetail : callOcrDetails){
            countUtils.countApi(type, callOcrDetail.getResult(), callOcrDetail.getStatus(), callOcrDetail.getFileUrl(), new AjTicketTask(){{
                setTicketId(callOcrDetail.getTicketId());
                setVersion(callOcrDetail.getVersion());
            }}, month);
            callApiDetailMapper.updateCallApiDetail(new AjCallApiDetail(){{
                setId(callOcrDetail.getId());
                setTrigger(1);
            }});
        }
    }
}
